<?php
// 本类由系统自动生成，仅供测试用途
namespace Api\Controller;

use Think\Controller;

class UserController extends PublicController
{

    Public function verify()
    {
        $image = new \Org\Util\Image;
        $image->buildImageVerify();
    }

    //***************************
    //  获取用户订单数量
    //***************************
    public function getorder()
    {
        $uid = intval($_REQUEST['userId']);
        if (!$uid) {
            echo json_encode(array('status' => 0, 'err' => '非法操作.'));
            exit();
        }

        $order = array();
        $order['pay_num'] = intval(M('order')->where('uid=' . intval($uid) . ' AND status=10 AND del=0')->getField('COUNT(id)'));
        $order['rec_num'] = intval(M('order')->where('uid=' . intval($uid) . ' AND status=30 AND del=0 AND back="0"')->getField('COUNT(id)'));
        $order['finish_num'] = intval(M('order')->where('uid=' . intval($uid) . ' AND status>30 AND del=0 AND back="0"')->getField('COUNT(id)'));
        $order['refund_num'] = intval(M('order')->where('uid=' . intval($uid) . ' AND back>"0"')->getField('COUNT(id)'));
        echo json_encode(array('status' => 1, 'orderInfo' => $order));
        exit();
    }

    public function findfwd_edit()
    {
        $name = $_POST['name'];
        $tel = $_POST['tel'];
        $newpwd = $_POST['newpwd'];
        $newpwds = $_POST['newpwds'];
        if (empty($name)) {
            $this->error('请输入用户名', U('User/findfwd', array('key' => $_REQUEST['key'])));
        }
        if (empty($tel)) {
            $this->error('请输入手机号', U('User/findfwd', array('key' => $_REQUEST['key'])));
        }
        if ($newpwd != $newpwds) {
            $this->error('两次密码输入不同', U('User/findfwd', array('key' => $_REQUEST['key'])));
        } else {
            $name = $_REQUEST['name'];        //帐号
            $tel = $_REQUEST['tel'];        //接受短信用户
            $yzm = $_REQUEST['yzm'];        //验证码
            $data['pwd'] = md5(md5($_REQUEST['newpwd'])); //新密码
            $sms_o = file_get_contents('Public/Rand/' . $tel . '.txt');
            if ($sms_o != $yzm) {
                $this->error('验证码错误！', U('User/findfwd', array('key' => $_REQUEST['key'])));
            } else {
                $result = M("user")->where('name = "' . $name . '"')->save($data);
                if ($result !== false) {
                    $this->success('修改成功！', U('User/logo', array('key' => $_REQUEST['key'])));
                } else {
                    $this->error('修改失败！', U('User/logo', array('key' => $_REQUEST['key'])));
                }
            }
        }
    }

    /**
     * 获取用户基本信息
     * @author tiedong
     * @date 2018-01-17
     */
    public function userInfo()
    {
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        $user = M("user")->where('id=' . intval($userId))->field('id,name,uname,photo,sex,birth,address,tel')->find();
        if ($user['photo'] && !strstr($user['photo'], "https://")) {
            $user['photo'] = __DATAURL__ . $user['photo'];
        }
        $user['tel'] = substr_replace($user['tel'], '****', 3, 4);
        $this->json_result($user);
        exit();
    }


    //***************************
    //  用户反馈接口
    //***************************
    public function feedback()
    {
        $uid = intval($_REQUEST['uid']);
        if (!$uid) {
            echo json_encode(array('status' => 0, 'err' => '登录状态异常.'));
            exit();
        }

        $con = $_POST['con'];
        if (!$con) {
            echo json_encode(array('status' => 0, 'err' => '请输入反馈内容.'));
            exit();
        }
        $data = array();
        $data['uid'] = $uid;
        $data['message'] = $con;
        $data['addtime'] = time();
        $res = M('fankui')->add($data);
        if ($res) {
            echo json_encode(array('status' => 1));
            exit();
        } else {
            echo json_encode(array('status' => 0, '保存失败！'));
            exit();
        }

    }

    //***************************
    //  用户商品收藏信息
    //***************************
    public function collection()
    {
        $user_id = intval($_REQUEST['id']);
        if (!$user_id) {
            echo json_encode(array('status' => 0, 'err' => '系统错误，请稍后再试.'));
            exit();
        }

        $pro_sc = M('product_sc');
        $count = $pro_sc->where('uid=' . intval($user_id))->count();// 查询满足要求的总记录数
        $Page = new \Org\Util\Page($count, 10);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出

        $sc_list = $pro_sc->where('uid=' . intval($user_id))->order('id desc')->select();
        foreach ($sc_list as $k => $v) {
            $pro_info = M('product')->where('id=' . intval($v['pid']) . ' AND del=0 AND is_down=0')->find();
            if ($pro_info) {
                $sc_list[$k]['pro_name'] = $pro_info['name'];
                $sc_list[$k]['photo'] = __DATAURL__ . $pro_info['photo_x'];
                $sc_list[$k]['price_yh'] = number_format($pro_info['price_yh'], 2);
            } else {
                $pro_sc->where('id=' . intval($v['id']))->delete();
            }
        }

        echo json_encode(array('status' => 1, 'sc_list' => $sc_list));
        exit();
    }

    //***************************
    //  用户单个商品取消收藏
    //***************************
    public function collection_qu()
    {
        $sc_id = intval($_REQUEST['id']);
        if (!$sc_id) {
            echo json_encode(array('status' => 0, 'err' => '非法操作.'));
            exit();
        }

        $product = M("product_sc");
        $ress = $product->where('id =' . intval($sc_id))->delete();
        //echo $shangchang->_sql();
        if ($ress) {
            echo json_encode(array('status' => 1));
            exit();
        } else {
            echo json_encode(array('status' => 0, 'err' => '网络异常！' . __LINE__));
            exit();
        }
    }


    //***************************
    //  用户单个店铺取消收藏
    //***************************
    public function unfollow()
    {
        $sc_id = intval($_REQUEST['id']);
        if (!$sc_id) {
            echo json_encode(array('status' => 0, 'err' => '非法操作.'));
            exit();
        }
        //取消关注店铺
        $shangchang = M("shangchang_sc");
        $ress = $shangchang->where('id =' . intval($sc_id))->delete();
        // echo $shangchang->_sql();
        if ($ress) {
            echo json_encode(array('status' => 1));
            exit();
        } else {
            echo json_encode(array('status' => 0, 'err' => '网络异常！' . __LINE__));
            exit();
        }

    }

    //***************************
    //  获取用户店铺收藏数据
    //***************************
    public function shangchang()
    {
        //关注店铺
        $user_id = intval($_REQUEST['user_id']);
        if (!$user_id) {
            echo json_encode(array('status' => 0, 'err' => '系统错误，请稍后再试.'));
            exit();
        }

        $shangchang = M("shangchang_sc");
        $count = $shangchang->where('uid=' . intval($user_id))->count();// 查询满足要求的总记录数
        $Page = new \Org\Util\Page($count, 4);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();// 分页显示输出

        $sc_list = $shangchang->where('uid=' . intval($user_id))->order('id desc')->select();
        foreach ($sc_list as $k => $v) {
            $sc_info = M('shangchang')->where('id=' . intval($v['shop_id']))->find();
            if ($sc_info) {
                $sc_list[$k]['shop_name'] = $sc_info['name'];
                $sc_list[$k]['uname'] = $sc_info['uname'];
                $sc_list[$k]['logo'] = 'http://' . $_SERVER['SERVER_NAME'] . __DATA__ . '/' . $sc_info['logo'];
                $sc_list[$k]['tel'] = $sc_info['tel'];
                $sc_list[$k]['sheng'] = M('china_city')->where('id=' . intval($sc_info['sheng']))->getField('name');
                $sc_list[$k]['city'] = M('china_city')->where('id=' . intval($sc_info['city']))->getField('name');
                $sc_list[$k]['quyu'] = M('china_city')->where('id=' . intval($sc_info['quyu']))->getField('name');
            } else {
                $shangchang->where('id=' . intval($v['id']))->delete();
            }
        }

        echo json_encode(array('status' => 1, 'sc_list' => $sc_list));
        exit();
    }

    /**
     * 头像修改
     * @author tiedong
     * @date 2018-01-17
     */
    public function uploadify()
    {
        $imgtype = array(
            'gif'  => 'gif',
            'png'  => 'png',
            'jpg'  => 'jpg',
            'jpeg' => 'jpeg'
        ); //图片类型在传输过程中对应的头信息
        $message = $_POST['message']; //接收以base64编码的图片数据
        $filename = $_POST['filename']; //自定义文件名称
        $ftype = $_POST['filetype']; //接收文件类型
        //首先将头信息去掉，然后解码剩余的base64编码的数据
        $message = base64_decode(substr($message, strlen('data:image/' . $imgtype[strtolower($ftype)] . ';base64,')));
        $filename2 = $filename . "." . $ftype;
        $furl = "./Data/UploadFiles/user_img/" . date("Ymd");
        if (!is_dir($furl)) {
            @mkdir($furl, 0777);
        }
        $furl = $furl . '/';

        //开始写文件
        $file = fopen($furl . $filename2, "w");
        if (fwrite($file, $message) === false) {
            echo json_encode(array('status' => 0, 'err' => 'failed'));
            exit;
        }

        ////图片URL地址
        $pic_url = $furl . $filename2;
        //$pic_url = "./Data/UploadFiles/user_img/20170115/0.jpeg";
        $image = new \Think\Image();
        $image->open($pic_url);
        // 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
        $image->thumb(100, 100, \Think\Image::IMAGE_THUMB_SCALE)->save($pic_url);
        /*echo $pic_url;
        exit();*/

        $uid = intval($_REQUEST['uid']);
        if (!$uid) {
            echo json_encode(array('status' => 0, 'err' => '登录状态异常！error'));
            exit();
        }
        //获取原来的头像链接
        $oldpic = M('user')->where('id=' . intval($uid))->getField('photo');
        $oldpic2 = './Data/' . $oldpic;

        $data = array();
        $data['photo'] = "UploadFiles/user_img/" . date("Ymd") . '/' . $filename2;
        $up = M('user')->where('id=' . intval($uid))->save($data);
        if ($up) {
            //如果原头像存在就删除
            if ($oldpic && file_exists($oldpic2)) {
                @unlink($oldpic2);
            }
            echo json_encode(array('status' => 1, 'urls' => 'Data/' . $data['photo']));
            exit();
        } else {
            echo json_encode(array('status' => 0, 'err' => '头像保存失败.'));
            exit();
        }

    }

    //***************************
    //  用户修改密码接口
    //***************************
    public function forget_pwd()
    {
        $user_name = trim($_REQUEST['username']);
        $tel = trim($_REQUEST['tel']);
        if (!$user_name || !$tel) {
            echo json_encode(array('status' => 0, 'err' => '请输入账号或手机号.'));
            exit();
        }

        $where = array();
        $where['name'] = $user_name;
        $where['tel'] = $tel;
        $check = M('user')->where($where)->count();
        if ($check) {
            echo json_encode(array('status' => 1));
            exit();
        } else {
            echo json_encode(array('status' => 0, 'err' => '账号不存在.'));
            exit();
        }
    }

    //***************************
    //  用户修改密码接口
    //***************************
    public function up_pwd()
    {
        $psw = trim($_POST['psw']);
        if (!$psw) {
            echo json_encode(array('status' => 0, 'err' => '请输入新密码.'));
            exit();
        }
        $user_name = trim($_POST['user']);
        $tel = trim($_POST['tel']);
        if (!$user_name || !$tel) {
            echo json_encode(array('status' => 0, 'err' => '系统错误，请稍后再试.'));
            exit();
        }

        $where = array();
        $where['name'] = $user_name;
        $where['tel'] = $tel;
        $pwd = md5(md5($psw));
        $up = M('user')->where($where)->save(array('pwd' => $pwd));
        if ($up) {
            echo json_encode(array('status' => 1));
            exit();
        } else {
            echo json_encode(array('status' => 0, 'err' => '账号不存在.'));
            exit();
        }
    }

    //***************************
    //  获取用户优惠券
    //***************************
    public function voucher()
    {
        $uid = intval($_REQUEST['uid']);
        if (!$uid) {
            echo json_encode(array('status' => 0, 'err' => '登录状态异常！' . __LINE__));
            exit();
        }

        //获取未使用或者已失效的优惠券
        $nouse = array();
        $nouses = array();
        $offdate = array();
        $offdates = array();
        $vou_list = M('user_voucher')->where('uid=' . intval($uid) . ' AND status!=2')->select();
        foreach ($vou_list as $k => $v) {
            $vou_info = M('voucher')->where('id=' . intval($v['vid']))->find();
            if (intval($vou_info['del']) == 1 || $vou_info['end_time'] < time()) {
                $offdate['vid'] = intval($vou_info['id']);
                $offdate['full_money'] = floatval($vou_info['full_money']);
                $offdate['amount'] = floatval($vou_info['amount']);
                $offdate['start_time'] = date('Y.m.d', intval($vou_info['start_time']));
                $offdate['end_time'] = date('Y.m.d', intval($vou_info['end_time']));
                $offdates[] = $offdate;
            } elseif ($vou_info['end_time'] > time()) {
                $nouse['vid'] = intval($vou_info['id']);
                $nouse['shop_id'] = intval($vou_info['shop_id']);
                $nouse['title'] = $vou_info['title'];
                $nouse['full_money'] = floatval($vou_info['full_money']);
                $nouse['amount'] = floatval($vou_info['amount']);
                if ($vou_info['proid'] == 'all' || empty($vou_info['proid'])) {
                    $nouse['desc'] = '店内通用';
                } else {
                    $nouse['desc'] = '限定商品';
                }
                $nouse['start_time'] = date('Y.m.d', intval($vou_info['start_time']));
                $nouse['end_time'] = date('Y.m.d', intval($vou_info['end_time']));
                if ($vou_info['proid']) {
                    $proid = explode(',', $vou_info['proid']);
                    $nouse['proid'] = intval($proid[0]);
                }
                $nouses[] = $nouse;
            }
        }

        ////获取已使用的优惠券
        $used = array();
        $useds = array();
        $vouusedlist = M('user_voucher')->where('uid=' . intval($uid) . ' AND status=2')->select();
        foreach ($vouusedlist as $k => $v) {
            $vou_info = M('voucher')->where('id=' . intval($v['vid']))->find();
            $used['vid'] = intval($vou_info['id']);
            $used['full_money'] = floatval($vou_info['full_money']);
            $used['amount'] = floatval($vou_info['amount']);
            $used['start_time'] = date('Y.m.d', intval($vou_info['start_time']));
            $used['end_time'] = date('Y.m.d', intval($vou_info['end_time']));
            $useds[] = $used;
        }

        echo json_encode(array('status' => 1, 'offdates' => $offdates, 'nouses' => $nouses, 'useds' => $useds));
        exit();
    }

    /**
     * 修改用户资料
     * @author tiedong
     * @date 2018-01-17
     */
    public function modify_data()
    {
        $userId = intval($this->get("id"));
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        $data = [];
        isset($_POST['name']) && $data['name'] = $_POST['name'];
        isset($_POST['sex']) && $data['sex'] = intval($_POST['sex']);
        isset($_POST['birth']) && $data['birth'] = $_POST['birth'];
        isset($_POST['address']) && $data['address'] = $_POST['address'];
        isset($_POST['photo']) && $data['photo'] = $_POST['photo'];

        $result = null;
        !empty($data) && $result = M("user")->where('id=' . $userId)->save($data);
        if ($result) {
            $this->json_result("操作成功！");
        } else {
            $this->json_error("操作失败");
        }
    }

    /**
     * 获取用户的心意卡
     * @author tiedong
     * @date 2018-01-23
     */
    public function getMyCard()
    {
        $userId = $this->get("id");
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        //('0'=>'已取消','10'=>'待付款','20'=>'已付款','30'=>'待发货','40'=>'已发货','50'=>'交易完成','51'=>'交易关闭');
        $where['status'] = array('IN', '20,30,40,50,51');
        $where['uid'] = $userId;
        $list = M('order')->where($where)->field("id,shop_id,vid,status,is_dui,suId")->select();
        foreach ($list as $key => $value) {
            if (empty($value['suid'])) {
                $list[$key]['song'] = 0;
            } else {
                $list[$key]['song'] = 1;
            }
            $list[$key]['name'] = M('brand')->where('id=' . $value['shop_id'])->getField('name');
            $list[$key]['brand_image'] = __DATAURL__ . "UploadFiles/" . M('brand_image')->where('id=' . $value['vid'])->getField('brand_photo');
        }
        $this->json_result($list);
    }

    /**
     * 用户点击兑换心意卡
     * @author tiedong
     * @date 2018-01-28
     */
    public function convertCard()
    {
        $userId = $this->get("id");
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        $order_id = !empty($_GET['order_id']) ? intval($_GET['order_id']) : '';
        $addr_id = !empty($_GET['addr_id']) ? intval($_GET['addr_id']) : '';
        if (empty($order_id) && empty($addr_id)) {
            $this->json_error("参数有误！！！");
            exit;
        }
        //获取地址
        $address = M("address")->where('id=' . $addr_id)->find();
        $data = array();
        /*$data['receiver'] = $address['name'];
        $data['tel'] = $address['tel'];
        $data['address_xq'] = $address['region_name'].$address['address'];
        $data['status'] = 30;
        $data['is_dui'] = '1';*/
        $receiver = $address['name'];
        $tel = $address['tel'];
        $address_xq = str_replace(',', '', $address['region_name']) . $address['address'];
        $status = 30;
        $is_dui = 1;
        $sql = "update lr_order set receiver='{$receiver}',tel='{$tel}' ,address_xq='{$address_xq}',status={$status},is_dui={$is_dui} WHERE id=" . $order_id;
        $result = M("order")->execute($sql);
        if ($result) {
            $return = "兑换成功";
        } else {
            $return = "兑换失败";
        }
        $this->json_result($return);
    }

    /**
     * 我的积分
     * @author tiedong
     * @date 2018-01-24
     */
    public function myJifen()
    {
        $signModel = M("user_sign");
        $userId = $this->get("id");
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        //判断用户今天是否已经签到
        $time = date("Y-m-d", time());
        $checkId = $signModel->where("FROM_UNIXTIME(addtime,'%Y-%m-%d')='$time' AND uid=" . $userId . " AND type=0")->getField("id");
        if ($checkId) {
            $text = "今日已签到";
        } else {
            $text = "未签到";
        }
        $jifen = M("user")->where('id=' . $userId)->getField("jifen");
        $ret = array(
            'text'  => $text,
            'jifen' => $jifen
        );
        $this->json_result($ret);
    }

    /**
     * 用户签到
     * @author tiedong
     * @date 2018-01-24
     */
    public function userSign()
    {
        $signModel = M("user_sign");
        $userId = $this->get("id");
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        //判断用户今天是否已经签到
        $time = date("Y-m-d", time());
        $checkId = $signModel->where("FROM_UNIXTIME(addtime,'%Y-%m-%d')='$time' AND uid=" . $userId . " AND type=0")->getField("id");
        if ($checkId) {
            $this->json_error("今日已经签到过了！");
            exit;
        }
        $data = array();
        $data['uid'] = $userId;
        $data['jifen'] = 10;
        $data['type'] = 0;
        $data['addtime'] = time();
        $result = $signModel->data($data)->add();
        if ($result) {
            M("user")->where('id=' . $userId)->setInc('jifen', 10);
            $this->json_result("今日签到成功");
            exit;
        } else {
            $this->json_error("今日签到失败！");
            exit;

        }
    }

    /**
     * 用户签到列表
     * @author tiedong
     * @date 2018-01-24
     */
    public function userSignList()
    {
        $signModel = M("user_sign");
        $userId = $this->get("id");
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        $list = $signModel->where('uid=' . $userId)->field("FROM_UNIXTIME(addtime,'%Y-%m-%d') as addtime,jifen,type,uid,id")->order("addtime DESC")->select();
        $this->json_result($list);
    }

    /**
     * 获取用户的收到心意卡
     * @author tiedong
     * @date 2018-01-23
     */
    public function getSMyCard()
    {
        $userId = $this->get("id");
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        //('0'=>'已取消','10'=>'待付款','20'=>'已付款','30'=>'待发货','40'=>'已发货','50'=>'交易完成','51'=>'交易关闭');
        $list = M('order')->where("suId=" . $userId)->field("id,shop_id,vid,status,is_dui")->select();
        foreach ($list as $key => $value) {
            $list[$key]['name'] = M('brand')->where('id=' . $value['shop_id'])->getField('name');
            $list[$key]['brand_image'] = __DATAURL__ . "UploadFiles/" . M('brand_image')->where('id=' . $value['vid'])->getField('brand_photo');
        }
        $this->json_result($list);
    }

    /**
     * 用户修改头像
     * @author tiedong
     * @date 2018-01-30
     */
    public function uploadHead()
    {
        $userId = $this->get("id");
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        $photoPath = $_GET['path'];
        $result = M("user")->where("id=" . $userId)->setField("photo", $photoPath);
        if ($result) {
            $this->json_result("上传头像成功！");
            exit;
        } else {
            $this->json_error("上传头像失败");
        }

    }

    public function address()
    {
        $userId = $this->get("id");
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        //所有地址
        $addressModel = M('address');
        $adds_list = $addressModel
            ->field("id,name,region_name,address,uid,is_default")
            ->where('uid=' . intval($userId))
            ->order('is_default desc,id desc')
            ->select();
        $this->json_result($adds_list);
        exit();

    }

    /**
     * 会员添加地址
     * @author tiedong
     * @date 2018-01-23
     */
    public function add_adds()
    {
        $userId = $this->get("id");
        if (!$userId) {
            $this->json_error("请登录");
            exit();
        }
        //查找该用户是否已经存在默认地址
        $check_default = M('address')->where("uid={$userId} and is_default=1")->getField('id');
        if ($check_default) {
            $this->json_error("默认地址已经存在！");
            exit();
        }
        $data = array();
        $data['name'] = trim($_GET['receiver']);
        $data['tel'] = trim($_GET['tel']);
        $data['region_name'] = $_GET['region_name'];
        $data['address'] = $_GET['adds'];
        //$data['code'] = $_POST['code'];
        $data['uid'] = intval($userId);
        if (!$data['name'] || !$data['tel'] || !$data['address']) {
            $this->json_error("请完善信息后再提交！！！");
            exit();
        }
        $check_id = M('address')->where($data)->getField('id');
        if ($check_id) {
            $this->json_error("该地址已经添加！");
            exit();
        }
        $data['is_default'] = intval($_GET['is_default']);
        /*$province = M('china_city')->where('id='.intval($data['sheng']))->getField('name');
        $city_name = M('china_city')->where('id='.intval($data['city']))->getField('name');
        $quyu_name = M('china_city')->where('id='.intval($data['quyu']))->getField('name');
        $data['address_xq'] = $province.' '.$city_name.' '.$quyu_name.' '.$data['address'];*/
        $res = M('address')->add($data);
        if ($res) {
            $this->json_result("添加地址成功！");
            exit();
        } else {
            $this->json_error("添加失败！");
            exit();
        }
    }

}